/*******************************************************************************
 * Copyright (c) 2023. 山东新北洋信息技术股份有限公司 保留所有权利。
 *
 ******************************************************************************/

package online.xuping.thousandsOfWorlds.common.constants;

/**
 * 入参错误码.
 */
public enum ParamErrorCodeEnum implements CommonErrorCodeHelper {
    //该一级项目已存在

    NOT_BLANK("NotBlank", "01", ErrorTypeCodeEnum.PARAM_ERROR),

    NULL("Null", "02", ErrorTypeCodeEnum.PARAM_ERROR),
    NOTNULL("NotNull", "03", ErrorTypeCodeEnum.PARAM_ERROR),
    NOT_EMPTY("NotEmpty", "04", ErrorTypeCodeEnum.PARAM_ERROR),
    PATTERN("Pattern", "05", ErrorTypeCodeEnum.PARAM_ERROR),

    LENGTH("Length", "06", ErrorTypeCodeEnum.PARAM_ERROR),
    MIN("Min", "07", ErrorTypeCodeEnum.PARAM_ERROR),
    MAX("Max", "08", ErrorTypeCodeEnum.PARAM_ERROR),
    DIGITS("Digits", "09", ErrorTypeCodeEnum.PARAM_ERROR),
    RANGE("Range", "10", ErrorTypeCodeEnum.PARAM_ERROR),
    EMAIL("Email", "11", ErrorTypeCodeEnum.PARAM_ERROR),

    OTHER(Constants.OTHER1, "99", ErrorTypeCodeEnum.PARAM_ERROR),
    ;

    /**
     * 错误码
     */
    private final String code;
    /**
     * 错误信息
     */
    private final String msg;

    /**
     * 构造方法
     *
     * @param msg               错误信息
     * @param code              code
     * @param errorTypeCodeEnum 类型枚举
     */
    ParamErrorCodeEnum(String msg, String code, ErrorTypeCodeEnum errorTypeCodeEnum) {
        this.code = ModelCodeEnum.WAREHOUSE.getCode() + errorTypeCodeEnum.getCode() +
                WarehouseBusinessCodeEnum.OTHER.getCode() + code;
        this.msg = msg;
    }

    /**
     * @param message      .
     * @param defaultValue .
     * @return .
     */
    public static String findCodeByMessage(String message, String defaultValue) {

        if (defaultValue == null) {
            defaultValue = Constants.OTHER1;
        }
        String defCode = "111111";
        if (message == null) {
            return defCode;
        }
        for (ParamErrorCodeEnum codeEnum : ParamErrorCodeEnum.values()) {
            if (message.equals(codeEnum.msg)) {
                return codeEnum.code;
            }
            if (defaultValue.equals(codeEnum.msg)) {
                defCode = codeEnum.code;
            }
        }

        return defCode;
    }

    /**
     * @param message .
     * @return .
     */
    public static String findCodeByMessage(String message) {
        return findCodeByMessage(message, Constants.OTHER1);
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    private static class Constants {
        public static final String OTHER1 = "OTHER";
    }
}
